<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        cache-key="systemUserTable"
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>

        <template #contact="{ row }">
          <el-tag
            v-for="item in row.contacts"
            :key="item.id"
            size="small"
            :disable-transitions="true"
            style="margin-right: 6px"
          >
            {{ item.contacts_name }}:{{ item.contacts_tel }}
          </el-tag>
          <br />
        </template>
        <template #status="{ row }">
          <el-switch
            size="small"
            :model-value="row.status === 1"
            @change="(checked) => editStatus(checked, row)"
          />
        </template>
        <template #action="{ row }">
          <router-link
            :to="'/company/position?id=' + row.id"
            style="text-decoration: none"
          >
            <el-link type="success" :underline="false"> 招人 </el-link>
          </router-link>
          <el-divider direction="vertical" />
          <el-link type="primary" :underline="false" @click="openEdit(row)">
            修改
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import Search from './components/search.vue';
  import Edit from './components/edit.vue';
  import { pageCompany } from '@/api/company';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      width: 90,
      label: 'ID'
    },
    {
      prop: 'city',
      label: '城市',
      width: 120
    },
    {
      prop: 'company_name',
      label: '企业名称',
      width: 220
    },
    {
      prop: 'contact',
      label: '联系人',
      width: 200,
      slot: 'contact'
    },
    {
      prop: 'lastVisit',
      label: '最后回访内容',
      minWidth: 120
    },
    {
      prop: 'job_times',
      label: '工作',
      width: 90,
      align: 'center',
      sortable: 'custom',
      slot: 'job_times'
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 200,
      align: 'center',
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageCompany({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
